var obj = new URLSearchParams(location.search)
var id = obj.get('id');
// console.log(id);
$.ajax({
    type: 'get',
    url: `/posts/${id}`,
    success: function (res) {
        var html = template('postTpl', res)
        $('.content .article').html(html)
    }
})

$('.content .article').on('click', '#like', function () {
    $.ajax({
        type: 'post',
        url: `/posts/fabulous/${id}`,
        success: function (res) {
            $('#like').html('赞:(' + res.meta.likes + ')')
            alert('点赞成功')
        }
    })
})

// 获取当前评论设置状态
var review;
$.ajax({
    type: 'get',
    url: '/settings',
    success: function (res) {
        review = res.review;
        console.log(res);
        var tpl = ` 
			<form>
	          <textarea></textarea>
	          <input type="submit" value="提交评论">
	        </form>
		`
        if (res.comment == true) {
            $('.comment').html(tpl)
        }
    }
})

$('.comment').on('submit', 'form', function () {
    var content = $(this).find('textarea').val();//评论的内容
    $.ajax({
        type: 'post',
        url: '/comments',
        data: {
            post: id,
            content: content,
            state: review == true ? 0 : 1
        },
        success: function () {
            alert('评论成功')
            location.reload()
        }
    })
    return false;
})